package com.bootdo.market.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.domain.PlatformStatisticsDataDO;
import com.bootdo.market.service.PlatformStatisticsDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author jinse
 * @email market@jinse.com
 * @date 2019-03-30 18:29:44
 */

@Controller
@RequestMapping("/market/platformStatisticsData")
public class PlatformStatisticsDataController {
    @Autowired
    private PlatformStatisticsDataService platformStatisticsDataService;

    @GetMapping()
    @RequiresPermissions("market:platformStatisticsData:platformStatisticsData")
    String PlatformStatisticsData(Model model) throws ParseException {
        model.addAttribute("creattime", getYesterdayDateTime(-1));
        return "market/platformStatisticsData/platformStatisticsData";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:platformStatisticsData:platformStatisticsData")
    public PageUtils list(@RequestParam Map<String, Object> params) throws ParseException {
        //查询列表数据
        if ("".equals(params.get("creattime")) || params.get("creattime") == null) {
            //默认获取前一天
            params.put("creattime", getYesterdayDateTime(-1));
        } else {
            params.put("creattime", params.get("creattime"));
        }
        Query query = new Query(params);
        List<PlatformStatisticsDataDO> platformStatisticsDataList = platformStatisticsDataService.list(query);
        int total = platformStatisticsDataService.count(query);
        PageUtils pageUtils = new PageUtils(platformStatisticsDataList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:platformStatisticsData:add")
    String add() {
        return "market/platformStatisticsData/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:platformStatisticsData:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        PlatformStatisticsDataDO platformStatisticsData = platformStatisticsDataService.get(id);
        model.addAttribute("platformStatisticsData", platformStatisticsData);
        return "market/platformStatisticsData/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:platformStatisticsData:add")
    public R save(PlatformStatisticsDataDO platformStatisticsData) {
        if (platformStatisticsDataService.save(platformStatisticsData) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:platformStatisticsData:edit")
    public R update(PlatformStatisticsDataDO platformStatisticsData) {
        platformStatisticsDataService.update(platformStatisticsData);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:platformStatisticsData:remove")
    public R remove(Integer id) {
        if (platformStatisticsDataService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:platformStatisticsData:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        platformStatisticsDataService.batchRemove(ids);
        return R.ok();
    }

    //获取时间，正数和负数分别代表后和前
    public static String getYesterdayDateTime(int num) throws ParseException {
        SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
        Date date = DATETIME_FORMAT.parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DATE, num);
        Date d = now.getTime();
        return DATETIME_FORMAT.format(d);
    }
}
